package com.xq.shop.controller.user;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xq.shop.po.Product;
import com.xq.shop.service.user.IBaseService;
import com.xq.shop.service.user.IProductService;
import com.xq.shop.util.UserServiceFactory;
import com.xq.shop.vo.SearchObject;

@Controller
@RequestMapping("/user/")
public class ProductCtrl extends BaseCtrl<Product> {

	private IProductService productService  = UserServiceFactory.getProductService();
	
	@Override
	public IBaseService<Product> getService() {
		return productService;
	}

	@Override
	@RequestMapping("product_list.do")
	@ResponseBody
	public String list(SearchObject search, HttpServletRequest request) {
		return super.list(search, request);
	}
	
	@RequestMapping("product_info.do")
	public ModelAndView info(String id){
		
		ModelAndView modelAndView = new ModelAndView("product/product_info");
		Product product;
		if(id != null){
			product = productService.get(Long.parseLong(id));
		} else {
			product = new Product();
		}
		
		if(product == null){
			//TODO goto 404
			return modelAndView;
		} else {
			modelAndView.addObject("entity", product);
			return modelAndView;
		}
	}
	
	@RequestMapping("test.do")
	@ResponseBody
	public String test(){
		
		return productService.list(null) + "";
	}
}
